package com.example.tccdemo.config;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @describte:注册RocketMq的生产者和消费者
 * @program:tcc-demo
 * @author:zhengdp
 * @create:2021-09-27 11:20:51
 **/
@Configuration
public class RocketMQconfig {

    // 生产者
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public DefaultMQProducer producer(){
        DefaultMQProducer producer = new
                DefaultMQProducer("paymentGroup");
        producer.setNamesrvAddr("localhost:9876");
        return producer;
    }

    // 消费者
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public DefaultMQPushConsumer consumer(
            @Qualifier("messageListener") MessageListenerConcurrently listener
    )
            throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("paymentConsumerGroup");
        consumer.setNamesrvAddr("localhost:9876");
        // Subscribe one more more topics to consume.
        consumer.subscribe("payment", "*");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener(listener);
        return consumer;
    }
}
